﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Media;
using Microsoft.VisualStudio.Shell;
using Epic.ComponentServices;

namespace KyuuBackground
{
    //[LocalizationResource]
    public class KyuuBackgroundOptionPage : DialogPage
    {
        [Category("Global"), MemberOrder(0)]
        [DefaultValue(0.5)]
        public double GlobalOpacity { get; set; }

        [Category("Picture"), MemberOrder(0)]
        [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
        public string PictureSource { get; set; }

        [Category("Picture"), MemberOrder(2)]
        [DefaultValue(HorizontalAlign.Right)]
        public HorizontalAlign PictureAlignmentX { get; set; }

        [Category("Picture"), MemberOrder(3)]
        [DefaultValue(VerticalAlign.Bottom)]
        public VerticalAlign PictureAlignmentY { get; set; }

        [Category("Picture"), MemberOrder(1)]
        [DefaultValue(ScaleMode.Uniform)]
        public ScaleMode PictureScaleMode { get; set; }

        #region basic structure

        private bool mSettingsInitialized = false;

        protected override IWin32Window Window
        {
            get
            {
                var win = base.Window;

                var pg = win as PropertyGrid;
                if (pg != null)
                {
                    pg.PropertySort &= ~PropertySort.Alphabetical;
                }

                return win;
            }
        }

        protected override void OnActivate(CancelEventArgs e)
        {
            if (!mSettingsInitialized)
                LoadSettings();

            base.OnActivate(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            mSettingsInitialized = false;
        }

        private void LoadSettings()
        {
            mSettingsInitialized = true;

            var section = KyuuBackgroundConfigurationManager.Section;
            GlobalOpacity = section.Opacity;

            var pic = section.Picture;
            PictureSource = pic.Source;
            PictureScaleMode = pic.ScaleMode;
            PictureAlignmentX = pic.AlignmentX;
            PictureAlignmentY = pic.AlignmentY;
        }

        protected override void OnApply(DialogPage.PageApplyEventArgs e)
        {
            //base.OnApply(e);

            SaveSettings();
        }

        private void SaveSettings()
        {
            mSettingsInitialized = true;

            var section = KyuuBackgroundConfigurationManager.Section;
            section.Opacity = GlobalOpacity;

            var pic = section.Picture;
            pic.Source = PictureSource;
            pic.ScaleMode = PictureScaleMode;
            pic.AlignmentX = PictureAlignmentX;
            pic.AlignmentY = PictureAlignmentY;

            KyuuBackgroundConfigurationManager.Save();
        }

        #endregion
    }
}
